package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.intercept.LoginInterceptor;
import com.atguigu.gmall.cart.mapper.CartMapper;
import com.atguigu.gmall.cart.pojo.Cart;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: xie.zh
 * @create: 2021.03.30   13:44
 * @version: 1.0
 */
@Controller
public class CartController {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CartService cartService;

    /**
     * 添加到购物车
     *
     * @param cart
     * @return
     */
    @GetMapping
    public String saveCart(Cart cart) {
        this.cartService.saveCart(cart);
        return "redirect:http://cart.gmall.com/addCart.html?skuId=" + cart.getSkuId() + "&count=" + cart.getCount();
    }

    /**
     * 添加购物车成功后回显
     *
     * @param skuId
     * @param count
     * @param model
     * @return
     */
    @GetMapping("addCart.html")
    public String quryCartBySkuId(@RequestParam("skuId") Long skuId,
                                  @RequestParam("count") Integer count,
                                  Model model) {
        Cart cart = this.cartService.quryCartBySkuId(skuId, count);
        model.addAttribute("cart", cart);
        return "addCart";
    }

    /**
     * 查看购物车
     *
     * @param model
     * @return
     */
    @GetMapping("cart.html")
    public String queryCarts(Model model) {
        List<Cart> cartList = this.cartService.queryCarts();
        model.addAttribute("carts", cartList);
        return "cart";
    }

    /**
     * 更新购物车数量
     *
     * @param cart
     * @return
     */
    @PostMapping("updateNum")
    @ResponseBody
    public ResponseVo updateNum(@RequestBody Cart cart) {
        this.cartService.updateNum(cart);
        return ResponseVo.ok();
    }

    /**
     * 更新购物车选中状态
     *
     * @param cart
     * @return
     */
    @PostMapping("updateStatus")
    @ResponseBody
    public ResponseVo updateStatus(@RequestBody Cart cart) {
        this.cartService.updateStatus(cart);
        return ResponseVo.ok();
    }

    /**
     * 更新购物车选中状态
     *
     * @param skuId
     * @return
     */
    @PostMapping("deleteCart")
    @ResponseBody
    public ResponseVo deleteCart(@RequestParam("skuId") Long skuId) {
        this.cartService.deleteCart(skuId);
        return ResponseVo.ok();
    }

    /**
     * 通过用户id同步redis数据到mysql
     *
     * @param userId
     * @return
     */
    @GetMapping("{userId}")
    @ResponseBody
    public ResponseVo deleteCartByUserIdFromMysql(@PathVariable("userId") String userId) {
        this.cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id", userId));
        return ResponseVo.ok();
    }


    /**
     * 根据用户id查询购物选中状态的购物记录
     *
     * @param userId
     * @return
     */
    @GetMapping("check/{userId}")
    @ResponseBody
    public ResponseVo<List<Cart>> queryUserCartWithCheck(@PathVariable("userId") Long userId) {
        List<Cart> carts = this.cartService.queryUserCartWithCheck(userId);
        return ResponseVo.ok(carts);
    }

    @GetMapping("test")
    @ResponseBody
    public String test() {
        System.out.println(LoginInterceptor.getUserInfo());
        return "hello test";
    }
}
